import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Actives } from '../../pages/active/actives';
import 'rxjs/add/operator/map';

@Injectable()
export class ActiveDataProvider {

  private readonly baseUrl: string = 'https://api.bmob.cn/1';
  private readonly httpOption : Object = {
  headers : new HttpHeaders(
    {
      'X-Bmob-Application-Id':'748054e798358354bf419b92c0dd9638',
      'X-Bmob-REST-API-Key':'452b75414d0a4920470d5ff482cb451d',
      'Content-Type':'application/json'
    }
  )};

  constructor(public http: HttpClient) {
    console.log('获取HTTP数据服务加载');
    
  }

  /**
   * 获取
   * @returns {Observable<Task[]>}    代办事项列表数据集合
   */
  getActives(): Observable<Actives[]>{
    return this.http.get<Actives[]>(`${this.baseUrl}/classes/actives`,this.httpOption).map(data => data["results"]);
  }

  /**
   * 更新
   *  @param actives
   * @returns {Observable<Actives>} 响应结果
   * */
  updateActives(actives : Actives): Observable<Actives>{
    
    let _actives = {title: actives.title, status: actives.status, description:
    actives.description};

    return this.http.put<Actives>(`${this.baseUrl}/classes/actives/${actives.objectId}`,
    _actives,this.httpOption);
  }

  /**
   * 删除
   * @param actives 
   * @returns {Observable<Actives>}   响应结果
   */
  deleteTask(actives : Actives):Observable<{}>{
    return this.http.delete(`${this.baseUrl}/classes/actives/${actives.objectId}`,
    this.httpOption);
  }

  /**
   * 新增
   * @param actives
   * @returns {Observable<Actives>} 响应结果
   */
  addActives(actives : Actives): Observable<Actives>{
    let _actives = {title: actives.title, status: actives.status, description:
      actives.description};
    return this.http.post<Actives>(`${this.baseUrl}/classes/actives/`,
    _actives,this.httpOption);
}

}
